/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.action.query;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.inspur.edp.bff.api.exception.BffFrameworkException;
import com.inspur.edp.bff.api.exception.ErrorCodes;
import com.inspur.edp.bff.api.manager.context.QueryContext;
import com.inspur.edp.cef.entity.condition.FilterCondition;
import com.inspur.edp.cef.entity.condition.SortCondition;
import com.inspur.edp.cef.spi.jsonser.base.StringUtils;

import java.io.IOException;
import java.util.ArrayList;

public abstract class BaseQueryConditionAction extends BeforeQueryAction {

    private ArrayList<FilterCondition> defaultFilterCondition;
    private ArrayList<SortCondition> defaultOrderByCondition;

    public BaseQueryConditionAction(QueryContext context) {
        super(context);
    }

    public BaseQueryConditionAction(String filterStr, String sortStr, QueryContext context) {
        super(context);

        ObjectMapper mapper = new ObjectMapper();
        try {
            if (!StringUtils.isNullOrEmpty(filterStr))
                defaultFilterCondition = mapper.readValue(filterStr, new TypeReference<ArrayList<FilterCondition>>() {
                });
        } catch (IOException e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_2021, filterStr);
        }

        try {
            if (!StringUtils.isNullOrEmpty(sortStr))
                defaultOrderByCondition = mapper.readValue(sortStr, new TypeReference<ArrayList<SortCondition>>() {
                });
        } catch (IOException e) {
            throw new BffFrameworkException(e, ErrorCodes.BFF_FRAMEWORK_2022, sortStr);
        }

    }

    public ArrayList<SortCondition> getDefaultOrderByCondition() {
        return defaultOrderByCondition;
    }

    public ArrayList<FilterCondition> getDefaultFilterCondition() {
        return defaultFilterCondition;
    }

    @Override
    protected void execute() {
        //默认的filter
        ArrayList<SortCondition> sortConditions = getDefaultOrderByCondition();
        ArrayList<FilterCondition> filterConditions = getDefaultFilterCondition();

        //获取外部传进来的filter
        ArrayList<SortCondition> sortConditions1 = getQueryContext().getFilter().getSortConditions();
        ArrayList<FilterCondition> filterConditions1 = getQueryContext().getFilter().getFilterConditions();
        //默认的filter
        getQueryContext().getFilter().setSortConditions(sortConditions);
        getQueryContext().getFilter().setFilterConditions(filterConditions);

        //加上外部传进来的filter
        if (sortConditions1 != null) {
            getQueryContext().getFilter().addSortConditions(sortConditions1);
        }
        if (filterConditions1 != null) {
            getQueryContext().getFilter().addFilterConditions(filterConditions1);
        }
    }
}
